﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using SuperStarProject.Models;
using System.Data.SqlClient;

namespace SuperStarProject.DAO
{
    public class NhaCungCapDAO
    {
        public static List<NhaCungCapModel> ShowAll()
        {
            SqlConnection db = DataProvider.DBconnection();
            SqlDataReader rdr = DataProvider.RunStoredProc_Read("sp_select_NhaCungCap", db);
            return Convert(rdr);
        }
        public static NhaCungCapModel ViewDetails(string id)
        {
            NhaCungCapModel prod = new NhaCungCapModel();
            SqlParameter para_productId = new SqlParameter("@ma", (object)id);

            SqlConnection db = DataProvider.DBconnection();

            List<SqlParameter> lstParameter = new List<SqlParameter>();
            lstParameter.Add(para_productId);

            SqlDataReader rdr = DataProvider.RunStoredProcParams_Read("sp_select_NhaCungCap_dk", db, lstParameter);
            if (rdr.HasRows)
                return Convert(rdr).First();
            else
                throw new OperationCanceledException();
        }
        static List<NhaCungCapModel> Convert(SqlDataReader rdr)
        {
            List<NhaCungCapModel> lstItem = new List<NhaCungCapModel>();
            while (rdr.Read())
            {
                NhaCungCapModel item= new NhaCungCapModel();
                item.MaNhaCungCap = (string)rdr["MaNhaCungCap"];
                item.TenNhaCungCap = (string)rdr["TenNhaCungCap"];
                lstItem.Add(item);
            }
            return lstItem;
        }
    }
}
